<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/2/20
 * Time: 9:13
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\api\model\Collect as collectModel;
use app\common\model\JlouOrderModel as jl;
class Jlou extends Nocommon
{
    public function _initialize()
    {
        $data = Db::name('jlou_goods')->field('id,date,time,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,is_kaijiang,number')
            ->where('is_del', 0)->where('is_display', 1)->select();

        foreach ($data as $k => $v) {
            if ($v['is_kaijiang'] == 0 && time() > strtotime($v['time'])) {
                if ($v['number'] < $v['shop_min_num']) {
                    Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 3);
                } else {
                    Db::name('jlou_goods')->where('id', $v['id'])->setField('is_kaijiang', 1);
                }

            }
        }
    }

    /*
     * 捡漏
     */
    public function index()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $page = $this->p();
            //捡漏
            $type = input('type', '1', 'trim');
            if ($type == 1) {
                $res = Db::name('jlou_goods')->field('id,date,time,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,number')->where('is_del', 0)->where('is_display', 1)
                    ->limit(($page - 1) * config('page_size'), config('page_size'))
                    ->order('date','desc')
                    ->select();
                foreach ($res as $k => $v) {
                    $res[$k]['time'] = strtotime($v['time']);
                    //查询参与数
                    // $res[$k]['count'] = Db::name('jlou_order')->where('goods_id',$v['id'])->count();
                    if (time() > strtotime($v['time'])) {
                        unset($res[$k]);
                    }

                }
                rsort($res);
            }
            if ($type == 2) {
                $res = Db::name('jlou_order')->alias('a')->field('id,date,shop_name,t_shop_name,is_kaijiang,count(id) as count,a.is_have,a.create_time as x_time')->
                join('jlou_goods b', 'a.goods_id=b.id', 'left')
                    ->where('a.uid', $post['uid'])
                    ->where('order_status', 1)
                    ->group('id')
                    ->limit(($page - 1) * config('page_size'), config('page_size'))
                    ->order('x_time','desc')
                    ->select();
                foreach ($res as $k => $v) {
                    if ($v['is_kaijiang'] == 0) {
                        $res[$k]['type'] = 1;
                    }
                    if ($v['is_kaijiang'] == 1) {
                        $res[$k]['type'] = 2;
                    }
                    if ($v['is_kaijiang'] == 2) {
                        $res[$k]['type'] = 3;
                    }
                    if ($v['is_kaijiang'] == 3) {
                        $res[$k]['type'] = 4;
                    }

                }
            }
            if ($type == 3) {
                $res = Db::name('jlou_goods')->alias('a')->field('id,date,t_shop_name,u.nickname,u.user_icon,t_shop_img,code,open_time')
                    ->join('jlou_order b', 'b.goods_id=a.id', 'left')
                    ->join('user u', 'b.uid=u.uid', 'left')
                    ->where('b.is_have', 1)
                    ->limit(($page - 1) * config('page_size'), config('page_size'))
                    ->order('date','desc')
                    ->select();
            }
            $this->apiReturn('200', '成功', $res);

        }
    }

    /*
     * 幸运得主
     */
    public function lucky()
    {
        if (Request::instance()->isPost()) {
            $res = Db::name('jlou_order')->alias('a')->field('u.nickname,b.t_shop_name')
                ->join('jlou_goods b', 'a.goods_id=b.id', 'left')
                ->join('user u', 'a.uid=u.uid', 'left')
                ->where('a.is_have', 1)
                ->select();
            $this->apiReturn('200', '成功', $res);
        }
    }

    /*
     * 捡漏详情
     *
     */
    public function jlou_details()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $type = input('type', '1', 'trim');
            //有购买参与按钮
            if ($type == 1) {
                //获取详情
                $res = Db::name('jlou_goods')->alias('a')->field('id,date,time,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,is_kaijiang,number')
                    ->where('id', $post['id'])
                    ->find();
                $res['time'] = strtotime($res['time']);
            }
            //开奖中
            if ($type == 2) {
                //获取详情
                $res = Db::name('jlou_goods')->alias('a')->field('id,date,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,is_kaijiang,number')
                    ->where('id', $post['id'])
                    ->find();
            }
            //查看是否中奖
            if ($type == 3) {
                $res = Db::name('jlou_goods')->alias('a')->field('id,date,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,is_kaijiang,number,code')
                    ->where('id', $post['id'])
                    ->find();
                //查询我是否中奖
                $user = Db::name('jlou_order')->field('order_code')->where('uid', $post['uid'])->select();
                $order = Db::name('jlou_order')->where('goods_id',$post['id'])->where('uid',$post['uid'])->select();
                if(!empty($order)){
                    if ($this->deep_in_array($res['code'], $user)) {
                        $res['is_have'] = 1;
                    } else {
                        $res['is_have'] = 2;
                    }
                }else{
                    $res['is_have'] = 2;
                }
            }
            //参与人数不足
            if ($type == 4) {
                $res = Db::name('jlou_goods')->alias('a')->field('id,date,shop_img,shop_name,
                shop_price,shop_min_num,shop_max_num,t_shop_img,t_shop_name,t_shop_price,is_kaijiang,number')
                    ->where('id', $post['id'])
                    ->find();
            }
            //查询所有的
            $this->apiReturn('200', '成功', $res);
        }
    }

    /*
     * 我的参与码
     */
    public function my_code()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            $res = Db::name('jlou_order')->field('order_code,create_time')
                ->where('uid', $post['uid'])
                ->where('order_status',1)
                ->where('goods_id',$post['id'])
                ->select();
            $this->apiReturn('200', '成功', $res);

        }
    }

    /**
     * [submitJlouOrder 捡漏支付]
     * @Author   kendy
     * @DateTime 2020-02-21T14:00:42+0800
     * @return   [type]                   [description]
     */
    public function submitJlouOrder()
    {
        if (Request::instance()->isPost()) {
            $jl = new jl();
            $result = $jl->submitJlOrder();
        }
    }

    public function payments()
    {

    }

    /*
    * 购买参与接口
    *
    */
    public function buy()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            //查询是否大于数量
            $res = Db::name('jlou_goods')->field('shop_max_num,number')->where('id', $post['id'])->find();
            if ($res) {
                if ($res['shop_max_num'] > $res['number']) {
                    $this->apiReturn('200', '成功');
                } else {
                    $this->apiReturn('400', '本期商品已购完');
                }
            } else {
                $this->apiReturn('401', '数据为空');
            }
        }
    }

public function yijian(){
  if (Request::instance()->isPost()) {
         $post = Request::instance()->post();
          $uid = input("post.uid");
          $page = $this->p(); 
          $tOrderIds = [];
          if(empty($uid))
          {
            $this->apiReturn('400', 'uid为空');
          }
            $is_real = empty(input('post.real'))?0:input('post.real'); 
            $where['is_real'] = 0;
            if($is_real == 1){
                $where['is_real'] = 1;
            }
            $result = $this->getgoodsList($where,$uid,$page);
            //普通商品
            if(count($result)>0){
                foreach ($result as $key => $value) {
                    $result[$key]['is_common'] = 1;
                    //特别商品查看捡漏订单表里is_have = 1,的商品id
                    if($value['is_have'] == 1){ 
                       $tOrderIds[] = $value['order_id'];
                    }
                } 
            } 
          if(count($tOrderIds)>0){ 
             $teshu = $this->gettgoodsList(['jo.order_id'=>['IN',$tOrderIds]],$uid);
                 //特殊商品
              if(count($teshu)>0){
                foreach ($teshu as $key => $value) {
                    $teshu[$key]['is_common'] = 0; 
                } 
            } 
            $a = array_merge($result,$teshu);
             foreach ($a as $key => $value) { 
                 $order_time[$key] = $value['order_id']; 
                }
              array_multisort($order_time,SORT_DESC,$a);  
              
              $this->apiReturn('200', '成功',$a);
            } 

       $this->apiReturn('200', '成功',$result);
      }
   }


    public function getgoodsList($where,$uid,$page){
           $result = Db::name('jlou_order') 
            ->alias('jo')
            ->field('jo.order_id,jg.id,jg.date,jg.shop_name,jg.shop_img,jo.is_send,dk_code,is_have,cancel_code,real_date')
            ->join('jlou_goods jg','jg.id = jo.goods_id','left')
            ->where(['order_status'=>1,'is_display'=>1,'is_del'=>0,'is_kaijiang'=>2,'jo.uid'=>$uid])
            ->where($where)
            ->limit(($page - 1) * config('page_size'), config('page_size'))
            ->select();
            return $result; 
    }

    public function gettgoodsList($where,$uid){
           $result = Db::name('jlou_order') 
            ->alias('jo')
            ->field('jo.order_id,jg.id,jg.date,t_shop_img as shop_img,t_shop_name as shop_name,jo.is_send,t_code as dk_code,t_is_send as is_send,is_have,cancel_code,real_date')
            ->join('jlou_goods jg','jg.id = jo.goods_id','left')
            ->where(['order_status'=>1,'is_display'=>1,'is_del'=>0,'is_kaijiang'=>2,'jo.uid'=>$uid])
            ->where($where)
            ->select();
            return $result; 
    }


}